import 'package:dartkit/dartkit.dart';
import 'package:test/test.dart';

void main() {
  group("版本号解析", () {
    test("三位数", () {
      final v = Version.parse("3.1.4");
      expect(v.major, 3);
      expect(v.minor, 1);
      expect(v.patch, 4);
    });

    test("两位数", () {
      final v = Version.parse("2.5");
      expect(v.major, 2);
      expect(v.minor, 5);
      expect(v.patch, 0);
    });
  });

  test("toString", () {
    final version = Version(3, 1, 4);
    expect(version.toString(), "3.1.4");
  });

  group("兼容性测试", () {
    test("主版本不同，无法兼容", () {
      final high = Version(3, 1, 4);
      final low = Version(2, 4, 5);
      expect(high.isCompatible(low), isFalse);
      expect(low.isCompatible(high), isFalse);
    });

    test("次版本不同，单向兼容", () {
      final high = Version(3, 4, 4);
      final low = Version(3, 1, 5);
      expect(high.isCompatible(low), isFalse);
      expect(low.isCompatible(high), isFalse);
    });

    test("修订版不同，完全兼容", () {
      final high = Version(3, 4, 4);
      final low = Version(3, 4, 8);
      expect(high.isCompatible(low), isTrue);
      expect(low.isCompatible(high), isTrue);
    });
  });

  group('版本比较测试', () {
    var v1 = Version(1, 0, 0);
    var v2 = Version(1, 0, 1);
    var v3 = Version(1, 1, 0);
    var v4 = Version(2, 0, 0);
    var v5 = Version(1, 0, 0); // 与v1相同

    test('相等运算符', () {
      expect(v1 == v5, isTrue);
      expect(v1 == v2, isFalse);
    });

    test('不等运算符', () {
      expect(v1 < v2, isTrue);
      expect(v2 > v1, isTrue);
      expect(v3 > v2, isTrue);
      expect(v4 > v3, isTrue);
      expect(v1 <= v5, isTrue);
      expect(v1 >= v5, isTrue);
      expect(v1 <= v2, isTrue);
      expect(v2 >= v1, isTrue);
    });

    test('compareTo方法', () {
      expect(v1.compareTo(v2), equals(-1));
      expect(v2.compareTo(v1), equals(1));
      expect(v1.compareTo(v5), equals(0));
      expect(v3.compareTo(v2), equals(1));
      expect(v4.compareTo(v3), equals(1));
    });
  });

  group('哈希码测试', () {
    var v1 = Version(1, 2, 3);
    var v2 = Version(1, 2, 3);
    var v3 = Version(1, 2, 4);

    test('相同版本有相同哈希码', () {
      expect(v1.hashCode, equals(v2.hashCode));
    });

    test('不同版本有不同哈希码', () {
      expect(v1.hashCode, isNot(equals(v3.hashCode)));
    });
  });
}
